#coding:utf8
import cherrypy
#import datetime
from ConnectMSSQL import ms

from head import print_head
from end import print_end

from utf8code import *

_js = """
        <script type="text/javascript">
            $(function() 
            {
                $("#backbut").click(function()
                {
                    exit();
                });
            });
        </script>
        """

_top = """
    <form method="get" action="addSB">
      <div id="div_operate">
        <table width="auto">
          <tr>
            <td class="class_td">设备编号</td>
            <td><font>(保存成功后将自动生成)</font></td>
            <td class="class_td">固定资产编号</td>
            <td><input type="text" name="gdzcbhtext" /></td>
            <td class="class_td">购买日期<font>*</font></td>
            <td><input class="Wdate" name="gmrqtext" id="gmrqtext" type="text" onClick="WdatePicker()"></td>
          </tr>
          <tr>
            <td class="class_td">设备类型<font>*</font>&nbsp;&nbsp;&nbsp;<a href="/sblx"><font>新增类型</font></a></td>
            <td><select name="sblxselt" id="sblxselt"><option></option>%s</select></td>
            <td class="class_td">品牌型号<font>*</font></td>
            <td><input type="text" name="ppxhtext" /></td>
            <td class="class_td">设备名称(S/N)</td>
            <td><input type="text" name="sbmctext" /></td>
          </tr>
          <tr>
            <td class="class_td">基本配置</td>
            <td colspan="5"><input type="text" name="jbpztext" placeholder="CPU,内存,硬盘,显卡,网卡,光驱..." /></td>
          </tr>
          <tr>
            <td class="class_td">网络配置</td>
            <td colspan="5"><input type="text" name="wlpztext" placeholder="MAC1/IP1,MAC2/IP2" /></td>
          </tr>
          <tr>
            <td class="class_td">随机附件</td>
            <td colspan="5"><input type="text" name="sjfjtext" placeholder="显示器,键盘,鼠标,打印机..." /></td>
          </tr>
          <tr>
            <td class="class_td" colspan="6">备注</td>
          </tr>
          <tr>
            <td colspan="6"><textarea name="bztextarea"></textarea></td>
          </tr>
        </table>
    </div>
      <div align="right">
        <input type="submit" id="savebut" value="保 存" />
        <input type="button" id="backbut" value="取消" />
        <input type="reset" id="resetbut" value="重置" />
      </div>
        """
           
_end = """
    </form>
"""



class SBXZ:
    @cherrypy.expose
    def index(self):
        #==============================加载设备类型=======================
        _mid_init_sblx = """
                <option>%s</option>
                """
        _mid_sblx = ''
        sql_sblx = 'SELECT SBLX FROM T_SBLX ORDER BY SBLX'
        for sblx in ms.ExecQuery(sql_sblx):
            _mid_sblx += _mid_init_sblx % sblx
        #================================================================

        return utf8code(print_head % ('设备档案卡新增','设备档案卡新增') + _js + (_top % _mid_sblx) + _end + print_end)

    @cherrypy.expose
    def addSB(self,gdzcbhtext,gmrqtext,sblxselt,ppxhtext,sbmctext,jbpztext,wlpztext,sjfjtext,bztextarea):
        if (len(gmrqtext) > 0) and (len(sblxselt) > 0) and (len(ppxhtext) > 0):
            if len(wlpztext.strip()) > 16:#MAC地址加上冒号一共17个字符，故设定条件大于16
                sql_check = "SELECT * FROM T_SB WHERE WLPZ LIKE '%s'" % wlpztext[:16]
                if ms.ExecQuery(sql_check):#查出数据则表示已存在相同MAC的设备,将不允许插入
                    return utf8code(print_head % ("操作提示","保存失败!-_-||") + """<body onLoad="errorBack()">已存在<font>相同MAC地址</font>的设备,请查找后再更新资料!正在返回上一页,请稍等......</body>""" + print_end)       
            if len(sbmctext) > 0:
                sql_check = "SELECT * FROM T_SB WHERE SBMC = '%s'" % sbmctext
                if ms.ExecQuery(sql_check):#查出数据则表示已存在相同S/N码的设备,将不允许插入
                    return utf8code(print_head % ("操作提示","保存失败!-_-||") + """<body onLoad="errorBack()">已存在<font>相同S/N码</font>的设备,请查找后再更新资料!正在返回上一页,请稍等......</body>""" + print_end)       
            sql = "EXECUTE PRC_ADD_SB '%s','%s','%s','%s','%s','%s','%s','%s','%s';" % (gdzcbhtext,gmrqtext,sblxselt,ppxhtext,sbmctext,jbpztext,wlpztext,sjfjtext,bztextarea)
            print sql
            ms.ExecNonQuery(sql)
            return utf8code(print_head % ("操作提示","保存成功!*^_^*") + "<body onLoad=" + """javascript:setTimeout('exit()',3000);""" + ">正在关闭此页,请稍等......</body>" + print_end)
        else:#必填中的项有为空的将什么也不执行
            return utf8code(print_head % ("操作提示","保存失败!-_-||") + """<body onLoad="errorBack()">带红色<font>*</font>号的为必填项,正在返回上一页,请稍等......</body>""" + print_end)